package com.intelligent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.pojo.ProcessManagement;
import com.intelligent.service.ProcessManagementService;
import com.intelligent.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 工艺管理控制器
 */
@RestController
@RequestMapping("/processManagement")
public class ProcessManagementController {

    @Autowired
    private ProcessManagementService processManagementService;

    /**
     * 分页查询工艺管理信息
     * @param current 当前页
     * @param size 每页大小
     * @param processCode 工艺编号
     * @param processName 工艺名称
     * @param productCode 产品编号
     * @param productName 产品名称
     * @param status 状态
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<IPage<ProcessManagement>> getProcessManagementPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String processCode,
            @RequestParam(required = false) String processName,
            @RequestParam(required = false) String productCode,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) Integer status) {

        Page<ProcessManagement> page = new Page<>(current, size);
        IPage<ProcessManagement> result = processManagementService.getProcessManagementPage(
                page, processCode, processName, productCode, productName, status);

        return Result.success(result);
    }

    /**
     * 添加工艺管理信息
     * @param processManagement 工艺管理信息
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result<Boolean> addProcessManagement(@RequestBody ProcessManagement processManagement) {
        try {
            boolean success = processManagementService.addProcessManagement(processManagement);
            if (success) {
                return Result.success("工艺管理信息添加成功");
            } else {
                return Result.error("工艺管理信息添加失败");
            }
        } catch (Exception e) {
            return Result.error("添加工艺管理信息时发生错误：" + e.getMessage());
        }
    }

    /**
     * 更新工艺管理信息
     * @param processManagement 工艺管理信息
     * @return 更新结果
     */
    @PutMapping("/update")
    public Result<Boolean> updateProcessManagement(@RequestBody ProcessManagement processManagement) {
        try {
            // 参数校验
            if (processManagement.getId() == null) {
                return Result.error("记录ID不能为空");
            }
            if (processManagement.getProcessCode() == null || processManagement.getProcessCode().trim().isEmpty()) {
                return Result.error("工艺编号不能为空");
            }
            if (processManagement.getProcessName() == null || processManagement.getProcessName().trim().isEmpty()) {
                return Result.error("工艺名称不能为空");
            }
            if (processManagement.getProductId() == null) {
                return Result.error("适用产品不能为空");
            }
            if (processManagement.getUnitId() == null) {
                return Result.error("单位不能为空");
            }

            boolean success = processManagementService.updateProcessManagement(processManagement);
            if (success) {
                return Result.success("工艺管理信息更新成功");
            } else {
                return Result.error("工艺管理信息更新失败");
            }
        } catch (Exception e) {
            return Result.error("更新工艺管理信息时发生错误：" + e.getMessage());
        }
    }

    /**
     * 删除工艺管理信息
     * @param id 记录ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteProcessManagement(@PathVariable Long id) {
        try {
            // 参数校验
            if (id == null) {
                return Result.error("记录ID不能为空");
            }

            boolean success = processManagementService.deleteProcessManagement(id);
            if (success) {
                return Result.success("工艺管理信息删除成功");
            } else {
                return Result.error("工艺管理信息删除失败");
            }
        } catch (Exception e) {
            return Result.error("删除工艺管理信息时发生错误：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询工艺管理信息
     * @param id 记录ID
     * @return 工艺管理信息
     */
    @GetMapping("/{id}")
    public Result<ProcessManagement> getProcessManagementById(@PathVariable Long id) {
        try {
            // 参数校验
            if (id == null) {
                return Result.error("记录ID不能为空");
            }

            ProcessManagement processManagement = processManagementService.getProcessManagementById(id);
            return Result.success("查询成功",processManagement);
        } catch (Exception e) {
            return Result.error("查询工艺管理信息时发生错误：" + e.getMessage());
        }
    }
}
